<?php

/**
 * This model contains all methods to handle AJAX requests of subscription/unsubscription
 * to a topic, and of publishion of a new message.
 * @author Christophe Sourisse
 */
class Model_PublishSubscribe
{

   /**
    * Has that user subscribed to that topic?
    * @param string $topics - the IDs of the topics some user subscribed to
    * @param int $topic_ID - the ID of the topic to test
    * @return boolean 
    */
   public function hasAlreadySubscribed($topics, $topic_ID) {
      $topics_array = preg_split("#,#", $topics);;
      $result = false;
      $i = 0;
      while (!$result & $i<count($topics_array)) {
         if ($topics_array[$i] == $topic_ID)
            $result = true;
         $i++;
      }
      return $result;
   }
   
   /**
    * Add the specified ID to the given string of topics' IDs.
    * @param string $topics - the IDs of the topics some user subscribed to, called by reference
    * @param int $topic_ID - the topic ID to add
    */
   public function addIDToTopicsString(&$topics, $topic_ID) {
      if ($topics == "") {
         $topics = $topic_ID;
      }
      else {
         if (!self::hasAlreadySubscribed($topics, $topic_ID)) {
            $topics = $topics.",".$topic_ID;
         }
      }
   }
   
   /**
    * Retrieve the specified ID from the given string of topics' IDs.
    * @param string $topics - the IDs of the topics some user subscribed to
    * @param int $topic_ID - the topic ID to retrieve
    * @return string - the new string of topics' IDs
    */
   public function retrieveIDFromTopicsString($topics, $topic_ID) {
      $result = "";
      // If there are some topics...
      if (strlen($topics) > 0) {
         
         // Split it and compute the new string without the considered ID
         $topics_array = preg_split("#,#", $topics);
         foreach ($topics_array as $topic) {
            if ($topic != $topic_ID)
               $result = $result.$topic.",";
         }
         
         // Eventually retrieve the last coma
         if (strrpos($result, ",") == count($result))
            $result = substr($result, 0, count($result));
      }
      return $result;
   }
   
   /**
    * Update in the database the topics' IDs' string of a given user.
    * @param int $user_id - the ID of the user to modify
    * @param string $user_topics - the new topics' IDs' string of the given user
    * @return int - the number of modified database's rows 
    */
   public function updateUserTopics($user_id, $user_topics) {      
      // Get the database connection
      $db = Zend_Registry::get("my_db");
      
      // Construct the query parameters
      $data = array('topics' => $user_topics);
      $where = "id = ".$user_id;
      
      // Compute & Execute the query
      return $db->update('users', $data, $where);
   }
   
   /**
    * Register a new post into the database.
    * @param string $post - the text of the post
    * @param int $author - the ID of the post's author
    * @param int $topic - the ID of the topic where the post was released
    */
   public function registerPost($post, $author, $topic) {     
      // Avoid SQLi & XSS problems
      $newpost = mysql_real_escape_string($newpost);
      $newpost = htmlentities($newpost);
      
      // Get the database connection
      $db = Zend_Registry::get("my_db");
      
      // Construct the new message, to be inserted
      $data = array(
          'text'   => $post,
          'author' => $author,
          'topic'  => $topic
      );
      
      // Compute & Execute the query
      $db->insert('messages', $data);
   }
   
   /**
    * Get all subscribers of a given user
    * @param int $user_ID - the ID of the considered user
    * @return array - an array containing all subscribers of that user 
    */
   public function getSubscribers($user_ID) {
      $db = Zend_Registry::get("my_db");
      $sql = "SELECT * FROM users WHERE LOCATE('$user_ID',topics)>0";
      $pre_result = $db->fetchAll($sql);
      $result = array();
      foreach ($pre_result as $topic) {
         if (self::hasAlreadySubscribed($topic["topics"], $user_ID)) {
            $result[] = $topic;
         }
      }
      return $result;
   }
   
   
}

